There is a known problem in which when an ejection target comes into contact with a nozzle surface, a nozzle formed on the nozzle surface is damaged, and a liquid ejection performance from the nozzle is deteriorated. To prevent this problem, proposed is to perform processing to detect a distance between a surface of the ejection target and the nozzle surface by a sensor, and adjust the distance when the ejection target is determined to be likely to come into contact with the nozzle surface based on detection results.